-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: preserve magicgui-decorated function parameter hints with ParamSpec #600
Conversation
Codecov ReportAll modified lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #600 +/- ##
==========================================
+ Coverage 87.85% 87.86% +0.01%
==========================================
Files 39 39
Lines 4561 4565 +4
==========================================
+ Hits 4007 4011 +4
Misses 554 554
☔ View full report in Codecov by Sentry. |
Hmm, I think this may have broken some downstream things. I believe adding an extra generic parameter to your classes changes the inheritance interface, because inheriting classes now have to add the corresponding parameter or it will be a runtime error: The commit in question is: 05c9292 |
It would be helpful if you could include the code you ran with edit: are you using |
This was happening with a class that inherited from
|
ok... this is a complicated issue to be sure and I'm still trying to determine where you will and won't get the error: But to provide some general high level hints: you can always use FunctionGui without parametrizing the generic. It's optional, and only necessary for static type hinting... though it does have implications for runtime as well: class MyFunctionGui(FunctionGui): ... Since this is only necessary for static type hinting, and not part of the actual subclass inheritance requirements, you can also guard your subclass behind TYPE_CHECKING from typing import TYPE_CHECKING, Generic, TypeVar
T = TypeVar("T")
class MyType(Generic[T]):
...
if TYPE_CHECKING:
mybase = MyType[int]
else:
mybase = MyType
class MySub(mybase):
... in the weeds...I admit I'm a bit amused/confused that from typing import Generic, MutableSequence, TypeVar
from magicgui.widgets import FunctionGui
T = TypeVar("T")
class MyType(Generic[T]): ...
fgui_cgi = FunctionGui.__class_getitem__
mt_cgi = MyType.__class_getitem__
print("FunctionGui", fgui_cgi, fgui_cgi.__module__)
print("MyType", mt_cgi, mt_cgi.__module__)
class MyType2(MutableSequence[T]): ...
print("MyType2", MyType2.__class_getitem__) prints:
So simply subclassing from bringing it back to something concrete. Why don't you tell me more specifically where you're running into this error. link to a repo if you have one... and I'll try to help you come up with a static typing solution you're happy with, but for runtime, none of this is necessary. magicgui does need to be able to update its static typing interface, and there's really no way to deprecate that. So this change isn't going to be reverted... but there are ways that you can support both old/new magicgui at runtime and achieve any static typing you want to (though for that you'll need to run mypy assuming certain versions of magicgui). thanks! |
This PR uses ParamSpec to preserve function type hints when decorated with
@magicgui
before this PR:
after: